package top.aoyudi.utils;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.*;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class ExcelUtil {
    /**
     * 获取excel文件的工作表
     * @param fileName 文件
     * @param sheetName 工作表名
     * @return Sheet
     */
    public static Sheet excelConnect(String fileName,String sheetName){
        // 创建文件输入流
        FileInputStream fileInputStream;
        try {
            fileInputStream = new FileInputStream(fileName);
        } catch (FileNotFoundException e) {
            System.out.println("找不到文件");
            return null;
        }
        // 创建工作簿对象
        Workbook workBook;
        try {
            workBook = WorkbookFactory.create(fileInputStream);
        } catch (IOException | InvalidFormatException e) {
            System.out.println("非xls或xlsx文件或无该工作表");
            throw new RuntimeException(e);
        }
        // 返回指定工作表
        return workBook.getSheet(sheetName);
    }
}
